package com.willy.ratingbar;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrSet;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by willy on 2017/5/5.
 */

public class ScaleRatingBar extends AnimationRatingBar {

    private static final long ANIMATION_DELAY = 15;
    private Map<Integer,AnimatorProperty> animatorMap = new HashMap<>();

    public ScaleRatingBar(Context context, AttrSet attrs) {
        super(context, attrs);
    }

    public ScaleRatingBar(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void emptyRatingBar() {
        if (mRunnable != null) {
            mHandler.removeAllEvent();
        }
        long delay = 0;
        for (final PartialView view : mPartialViews) {
            mHandler.postTask(new Runnable() {
                @Override
                public void run() {
                    view.setEmpty();
                }
            }, delay += 5);
        }
    }

    @Override
    protected void fillRatingBar(final float rating) {
        // Need to remove all previous runnable to prevent emptyRatingBar and fillRatingBar out of sync
        if (mRunnable != null) {
            mHandler.removeAllEvent();
        }
        double maxIntOfRating = Math.ceil(rating);
        for (final PartialView partialView : mPartialViews) {
            final int ratingViewId = (int) partialView.getTag();
            if (ratingViewId > maxIntOfRating) {
                partialView.setEmpty();
                continue;
            }
            mRunnable = getAnimationRunnable(rating, partialView, ratingViewId, maxIntOfRating);
            postRunnable(mRunnable, ANIMATION_DELAY);
        }
    }

    private Runnable getAnimationRunnable(final float rating, final PartialView partialView, final int ratingViewId, final double maxIntOfRating) {
        return new Runnable() {
            @Override
            public void run() {
                getContext().getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        if (ratingViewId == maxIntOfRating) {
                            partialView.setPartialFilled(rating);
                        } else {
                            partialView.setFilled();
                        }
                        if (ratingViewId == rating) {
                            AnimatorProperty animatorProperty;
                            int tag = (int)partialView.getTag();
                            boolean key = animatorMap.containsKey(tag);
                            if (key){
                                animatorProperty = animatorMap.get(tag);
                            }else{
                                animatorProperty = partialView.createAnimatorProperty();
                                animatorMap.put(tag,animatorProperty);
                            }
                            animatorProperty.stop();

                            animatorProperty.scaleX(1.0f).scaleX(1.3f).scaleY(1.0f).scaleY(1.3f).setDuration(500);
                            animatorProperty.setStateChangedListener(new Animator.StateChangedListener() {
                                @Override
                                public void onStart(Animator animator) {

                                }

                                @Override
                                public void onStop(Animator animator) {
                                    animatorProperty.reset();
                                }

                                @Override
                                public void onCancel(Animator animator) {
                                    animatorProperty.reset();
                                }

                                @Override
                                public void onEnd(Animator animator) {
                                    animatorProperty.reset();
                                }

                                @Override
                                public void onPause(Animator animator) {

                                }

                                @Override
                                public void onResume(Animator animator) {

                                }
                            });
                            animatorProperty.start();
                        }
                    }
                });
            }
        };
    }
}

